﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace USB_Backup
{
    static class Program
    {
        /*
         * Sam Leach 2014
         * Utility designed for use in schools to prompt Students to automatically backup removable storage devices
         * "USB Backup"
         * 
         */

        [STAThread]
        static void Main(string[] args) //arguments should have our drive letter
        {
            string driveLetter = "";
            if (args.Length < 1) //no path has been passed
            {
                MessageBox.Show("No destination drive passed!\n Exiting");
                Environment.Exit(100);
            }
            else if (args.Length > 1)//too much as been passed
            {
                MessageBox.Show("More than one destination drive passed\n Exiting");
                Environment.Exit(100);
            }
            else if (args[0].Length ==1) //only one variable, is it a drive letter?
            {
                driveLetter = args[0];
            }
            else
            {
                MessageBox.Show("Please enter a single drive letter\n Exiting");
                Environment.Exit(100);
            }
            bool jobDone = false;//of course it isnt done, we have only just started
            while (!jobDone)// loop will keep running until a removable storage device is found
            {
                var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
                int count = 0;
                foreach (DriveInfo d in drives)
                {
                    count++;

                }
                if (count > 0)//there are drives, wohoo
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1(driveLetter));//lets get that form going, confusingly driveLetter is the destination drive!
                    jobDone = true;

                }
                else if (count == 0)//nothng there, wait for a bit and try again
                {
                    Thread.Sleep(5000);
                }
            }
            
        }
    }
}
